﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using System.IO;

public partial class Admin_Pages_ImageDelete : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["login"] != null)
            {
                try
                {
                    int imageid = Convert.ToInt32(Request.Params["imageid"]); 
                    ImagesUI imageUI = new ImagesUI();
                    string urlImage = imageUI.Get(imageid).Tables[0].Rows[0]["I_URL"].ToString();
                    int result = imageUI.Delete(imageid);
                    if (result > 0)
                    {
                        string url = Server.MapPath("~/"+urlImage);
                        if (File.Exists(url))
                        {
                            File.Delete(url);
                        }
                        Response.Write("<script>alert('Delete Successful'); window.location='../Pages/ImageList.aspx'; </script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('Exists Data Delete Fail'); window.location='../Pages/ImageList.aspx'; </script>");
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('Exists data, could not delete'); window.location='../Pages/CustomerList.aspx'; </script>");
                }
            }
            else
            {
                Response.Redirect("../Login.aspx");
            }

        }
    }
}
